//
//  ZDoctorAnswerViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/21.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZDoctorAnswerViewController.h"
#import "ZFAQsTableViewCell.h"
#import "ZDoctorHModel.h"
#import "ZLookFriendDeatilViewController.h"
static NSString * const KZDoctorAnswerViewIdentifier = @"KZDoctorAnswerViewIdentifier";
static  int Page = 1;
@interface ZDoctorAnswerViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView * _mainTabView;
}
@property (nonatomic,strong) NSMutableArray <ZDoctorHModel*>* modelArray;

@end

@implementation ZDoctorAnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self initlizeInterface];
    
}

- (void)initialzeDatas {
    self.modelArray = [@[] mutableCopy];
}
- (void)initlizeInterface {
    _mainTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _mainTabView.rowHeight = 145*ysIZE;
    _mainTabView.delegate = self;
    _mainTabView.dataSource = self;
    _mainTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mainTabView registerClass:[ZFAQsTableViewCell class] forCellReuseIdentifier:KZDoctorAnswerViewIdentifier];
    _mainTabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self lookNetWorking];
        [_mainTabView.mj_header endRefreshing];
    }];
    _mainTabView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self moreLookNetWorking];
        [_mainTabView.mj_footer endRefreshing];
    }];
    [self.view addSubview:_mainTabView];
    [_mainTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [_mainTabView.mj_header beginRefreshing];
}
//表格视图协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray ? _modelArray.count : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZFAQsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:KZDoctorAnswerViewIdentifier forIndexPath:indexPath];
//    cell.contextLabel .text = @"无言独上西楼寂寞梧桐深院锁清秋";
    cell.fModel = _modelArray[indexPath.row];
    cell.selectionStyle = 0;
    return  cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLookFriendDeatilViewController * vc = [[ZLookFriendDeatilViewController alloc] init];
    vc.qId = _modelArray[indexPath.row].qid;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)lookNetWorking {
    ESWeak(self, iWeakself);
    Page = 1;
    [YJProgressHUD showProgress:@"正在刷新" inView:self.view];
    NSString * p = [NSString stringWithFormat:@"%d",Page];
    NSDictionary * dict = @{@"p":p,@"did":@"1"};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/moreProblemList" sucessBlock:^(id content) {
        NSDictionary * iDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"%@",iDict);
        
        if ([iDict[@"code"] intValue] == 1 ) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [iWeakself disposeDataWithArray:iDict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"请求出错" inView:self.view];
        }
        
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求错误" inView:self.view];
    }];
}

- (void)disposeDataWithArray:(NSArray * )array {
    [_mainTabView.mj_footer resetNoMoreData];
   
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_mainTabView.mj_footer endRefreshingWithNoMoreData];
    
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_mainTabView.mj_footer endRefreshingWithNoMoreData];
    }
    [_modelArray removeAllObjects];
    for (NSMutableDictionary * dict in array) {
        ZDoctorHModel * model = [[ZDoctorHModel alloc] initWithDict:dict];
        [_modelArray addObject:model];
    }
    
    [_mainTabView reloadData];
    
}

- (void)moreLookNetWorking {
    Page ++;
    ESWeak(self, iWeakSelf);
    NSString * p = [NSString stringWithFormat:@"%d",Page];
    NSDictionary * dict = @{@"p":p,@"did":@"1"};
    [YJProgressHUD showProgress:@"正在加载" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/moreProblemList" sucessBlock:^(id content) {
        NSDictionary * idict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([idict[@"code"]  intValue] == 1) {
            [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
            [iWeakSelf moreDisposeDataWithArray:idict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"刷新失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求错误" inView:self.view];
    }];
}
- (void)moreDisposeDataWithArray:(NSArray * )array {
    [_mainTabView.mj_footer resetNoMoreData];
    if (array.count == 0) {
        [YJProgressHUD showMessage:@"已无更多案例" inView:self.view];
        [_mainTabView.mj_footer endRefreshingWithNoMoreData];
        return;
    }else if (array.count < 10) {
        [YJProgressHUD showMessage:@"刷新成功" inView:self.view];
        [_mainTabView.mj_footer endRefreshingWithNoMoreData];
    }
    [_modelArray removeAllObjects];
    for (NSMutableDictionary * dict in array) {
        ZDoctorHModel * model = [[ZDoctorHModel alloc] initWithDict:dict];
        [_modelArray addObject:model];
    }
    [_mainTabView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
